#include <stdio.h>
#include <stdlib.h>
/*
 *fibonacci数列
 *数据排序:冒泡 选择法 快排
 *进制转换
 *删除法求质数 
 * */

static void fibonacci_test(void)
{
	//求fibnacci数列前10个
	int fib[10] = {1,1};
	int i = 0;
	int temp = 0;
	int fib_len = sizeof(fib)/sizeof(fib[0]);
	for (i = 0;i<fib_len;i++)  
	{
		if (i>1)
		{
			fib[i] = fib[i-1]+fib[i-2];	
		}
		printf("fib[%d] = %d\n",i,fib[i]);
	}

	//逆序存放
	for (i = 0;i< fib_len/2;i++)
	{
		temp = fib[fib_len-1-i]; 
		fib[fib_len-1-i] =fib[i];
		fib[i] = temp;

	}
	
	for(i = 0;i<fib_len;i++)
	{
		printf("fib[%d] = %d\n",i,fib[i]);
	}
}

#define N 10
static void sort1(void)
{
	int a[N] = {12,8,45,30,98,68,2,7,68,11};
	int i ,j,temp;
	for(i = 0;i<N-1;i++)
	{
		for(j=0;j< N-1-i;j++)//结果 从尾锁定
		{
			if(a[j]>a[j+1])
			{
				temp = a[j];
				a[j] = a[j+1];
				a[j+1] = temp;
			}
		
		}


	}

	for(i=0;i<N;i++)
	{
		printf("a[%d] = %d\n",i,a[i]);

	}
}

static void sort2(void)
{
	int a[N] = {12,8,45,30,98,68,2,7,68,11};
	int i = 0,j = 0,temp = 0;
	for (i=0;i<N;i++)
	{
		printf("a[%d] = %d\n",i,a[i]);
	}

	for (i=0;i<N-1;i++)//N-1轮
	{
		
		for (j=i+1;j<N;j++)//结果 从头锁定
		{
			if (a[i]>a[j])	
			{
				temp = a[i];
				a[i] = a[j];
				a[j] = temp;	
			}						

		}

	}


	for (i=0;i<N;i++)
	{
		printf("a[%d] = %d\n",i,a[i]);
	}


}



/*
 *进制转换：从终端输入需要转换的值(十进制) 以及进制
 * */
#define INTARRSIZE 128
static void base_convert(void)
{
	int num = 0,mode = 10;
	int arr[INTARRSIZE];
	int i = 0;
	printf("plz enter num :\n");
	scanf("%d",&num);
	printf("plz enter mode :\n");
	scanf("%d",&mode);
	for(;;)
	{	
		arr[i] = num%mode;
		num = num/mode;
		i++;
		if(num == 0)
		{
			break;
		}
	}
	//倒序打印
	for(i-- ;i>=0 ;i--)
	{
		if (arr[i] > 10)
		{
			printf("%c",arr[i]-10+'a');
		}
		else
		{
			printf("%d",arr[i]);
		}
	}
	printf("\n");
}


/*
 *删除法求质数(先删除2的倍数再删除3倍 再删。。。。)
 * 每删一次（某个倍数的全部）  剩下的部分以最开始的数的倍数接着删
 * */
static void primer(void)
{
	char primer[1001] = {0};
	int i = 0,j = 0;
	for (i=2;i<1001;i++)
	{
		if (primer[i] == 0)
		{
			for(j= i*2;j<1001;j+=i)
			{
				primer[j] = -1;
	
			}	
		}
	

	}

	for(i = 2;i<1001;i++)
	{
		if (primer[i] == 0)
		{
			printf("%d is primer\n",i);
		}

//	printf("%d ",primer[i]);
	}
}



int main(void)
{
	//fibonacci_test();
	//sort1();	
	//sort2();
	
	//base_convert();
	
	primer();
	
	exit(0);
}
